﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace ProjectPacker
{
    public partial class ConfigForm : Form
    {
        MyConfig mc;

        public ConfigForm(ref MyConfig _mc)
        {
            InitializeComponent();
            mc = _mc;
        }

        private void button_Select_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                mc.FolderPath = folderBrowserDialog.SelectedPath;
                listBox_Folder.Items.Clear();
                AddFiletoListBox();
            }
        }
        private void AddFiletoListBox()
        {
            try
            {
                DirectoryInfo dir = new DirectoryInfo(mc.FolderPath);
                GetAllInfo(dir, listBox_Folder.Items);
            }
            catch { }//出错以后继续执行..
        }
        private void GetAllInfo(DirectoryInfo dir, IList items)
        {
            items.Add("0:" + dir.FullName);
            FileInfo[] files = dir.GetFiles();
            foreach (FileInfo f in files)
            {
                items.Add("1:" + f.FullName);
            }
            DirectoryInfo[] dirs = dir.GetDirectories();
            if (dirs.Count<DirectoryInfo>() != 0)
            {
                foreach (DirectoryInfo d in dirs)
                {
                    GetAllInfo(d, items);
                }
            }
        }

        private void ConfigForm_Load(object sender, EventArgs e)
        {
            Init();
        }
        private void Init()
        {
            folderBrowserDialog.SelectedPath = mc.FolderPath;
        }

        private void button_Add_Click(object sender, EventArgs e)
        {
            foreach (String s in listBox_Folder.SelectedItems)
            {
                if (listBox_Pack.FindStringExact(s.Replace(mc.FolderPath, "") + ":" + s.Substring(2)) == ListBox.NoMatches)
                {
                    listBox_Pack.Items.Add(s.Replace(mc.FolderPath, "") + ":" + s.Substring(2));
                    listBox_Pack.SetSelected(listBox_Pack.Items.Count - 1, true);
                }
            }
        }

        private void button_Delete_Click(object sender, EventArgs e)
        {
            String[] items = new String[listBox_Pack.SelectedItems.Count];
            listBox_Pack.SelectedItems.CopyTo(items, 0);
            foreach (String s in items)
            {
                listBox_Pack.Items.Remove(s);
            }
        }

        private void button_SaveConfig_Click(object sender, EventArgs e)
        {
            saveFileDialog1.InitialDirectory = Application.StartupPath + MyConfig.DataFolder;
            saveFileDialog1.FileName = "";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                List<String> items = new List<String>();
                foreach (String item in listBox_Pack.Items)
                {
                    items.Add(item);
                }
                DataManager.StringListSave(items, saveFileDialog1.FileName);
            }
        }

        private void button_LoadConfig_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = Application.StartupPath + MyConfig.DataFolder;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                listBox_Pack.Items.Clear();
                foreach (String s in DataManager.StringListLoad(openFileDialog1.FileName))
                {
                    listBox_Pack.Items.Add(s);
                }
            }
        }

        private void toolStripMenuItem_Edit_DropDownOpening(object sender, EventArgs e)
        {
            toolStripTextBox_Edit_1.TextBox.Text = "";
            toolStripTextBox_Edit_2.TextBox.Text = "";
            if (listBox_Pack.SelectedItems.Count == 1)
            {
                String[] s;
                s = listBox_Pack.SelectedItem.ToString().Split(new Char[] { ':' }, 3);
                toolStripTextBox_Edit_1.TextBox.Text = s[1];
                toolStripTextBox_Edit_2.TextBox.Text = s[2];
            }

        }

        private void toolStripMenuItem_Delete_Click(object sender, EventArgs e)
        {
            button_Delete_Click(sender, e);
        }

        private void toolStripTextBox_Edit_1_TextChanged(object sender, EventArgs e)
        {
            if (toolStripTextBox_Edit_1.TextLength != 0)
            {
                String[] s;
                s = listBox_Pack.SelectedItem.ToString().Split(new Char[] { ':' }, 3);
                s[1] = toolStripTextBox_Edit_1.TextBox.Text;
                int index = listBox_Pack.SelectedIndex;
                listBox_Pack.Items.RemoveAt(index);
                listBox_Pack.Items.Insert(index, s[0] + ":" + s[1] + ":" + s[2]);
                listBox_Pack.SetSelected(index, true);
            }

        }

    }
}
